﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Edu.Web.Data.DV;
using Edu.Web.Data.Entity;

namespace Edu.Web.Data.DAO.Impl
{
    public partial class StudentDAO
    {
        public StudentInfoDV FindInfoByStudentId(long studentId)
        {
            var query = from user in Db.Set<UserEntity>()
                        join stu in Db.Set<StudentEntity>() on user.Id equals stu.UserId
                        where stu.UserId == studentId
                        orderby user.CreateTime descending
                        select new StudentInfoDV
                        {
                            CreateTime = user.CreateTime,
                            Gender = user.Gender,
                            RealName = user.RealName,
                            UserName = user.UserName,
                            StuNo = stu.StuNo
                        };

            return query.FirstOrDefault();
        }

        public IQueryable<ClassStudentDV> LoadStudentListByClazzId(long clazzId)
        {
            return from clazz in Db.Set<ClazzEntity>()
                   join clazzStu in Db.Set<ClazzStuEntity>() on clazz.Id equals clazzStu.ClazzId into clazzStuSet
                   from clazzStuItem in clazzStuSet
                   join stu in Db.Set<StudentEntity>() on clazzStuItem.StuUserId equals stu.UserId into newStuSet
                   from newStuSetItem in newStuSet
                   join user in Db.Set<UserEntity>() on newStuSetItem.UserId equals user.Id
                   where clazzStuItem.ClazzId == clazzId
                   orderby newStuSetItem.StuNo ascending
                   select new ClassStudentDV
                   {
                       Gender = user.Gender,
                       UserId = user.Id,
                       StuNo = newStuSetItem.StuNo,
                       RealName = user.RealName
                   };
        }
    }
}